<template>
    <div class="wrapper">
        <top-header title="我卖的车" title-left="添加" @leftClick="addCar"></top-header>
        <list class="item-container"
                offset-accuracy="50px">
            <refresh @loadingDown="loadingDown(index)"></refresh>
            <cell class="border-cell"></cell>
            <cell v-for="(demo,key) in carArray"
                    class="cell"
                    :key="key">
                <div class="content"  @click="carDetail(demo.pId)">
                    <image class="content_img" v-if="demo.headImg" :src="demo.headImg"></image>
                    <image class="content_img" v-else src="bmlocal://assets/acw/car.jpg"></image>
                    <div>
                        <text class="content_first">{{demo.pAllname}}</text>
                        <div class="content_two">
                            <text class="content_two1">{{demo.pUsername}}</text>
                            <text class="content_two1" >{{demo.pTel}}</text>
                        </div>
                        <div class="content_third">
                            <text class="content_third_price">售价：{{demo.pPrice}}</text>
                            <text class="content_third_price">状态：{{demo.isshow ? "上架" :"下架"}}</text>
                        </div>
                        <div class="content_four">
                            <!-- <WxcButton @wxcButtonClicked="toUpdate(demo.pId)" class="content_four_text" type="white" size="medium" text="编辑"></WxcButton> -->
                            <WxcButton @wxcButtonClicked="openDialog(demo, 1)"  class="content_four_text" type="white" size="medium" text="上架"></WxcButton>
                            <WxcButton @wxcButtonClicked="openDialog(demo, 2)"  class="content_four_text" type="white" size="medium" text="下架"></WxcButton>
                            <WxcButton @wxcButtonClicked="openDialog(demo, 3)" class="content_four_text" type="white" size="medium" text="删除"></WxcButton>
                        </div>
                    </div>
                </div>
            </cell>
            <cell class="cell" v-if="carArray.length <= 0">
                <div class="no-order">
                    <text>暂无车源</text>
                </div>
            </cell>
            <loading class="loading" @loading="onloading(index)" :display="showLoading">
                <loading-indicator class="comm-indicator" v-if="!lastPage"></loading-indicator>
                <text class="comm-indicator-text" >{{loadingText}}</text>
            </loading>
        </list>
        <wxc-dialog :title="title"
                :content="content"
                :confirm-text="confirmText"
                :cancel-text="cancelText"
                :show="show"
                :single="single"
                :is-checked="isChecked"
                :no-prompt-text="noPromptText"
                :show-no-prompt="showNoPrompt"
                @wxcDialogCancelBtnClicked="dialogCancelBtnClick"
                @wxcDialogConfirmBtnClicked="dialogConfirmBtnClick"></wxc-dialog>
    </div>
</template>
<script>
import { WxcButton, WxcDialog } from 'weex-ui';
import header from '../common/header'
export default {
    components: { 
        WxcButton,
        WxcDialog,
        'top-header': header, 
    },
    data () {
        return {
            token: '',
            carArray: [],
            userId: '',
            params: {
                uid: '',
                page: 1,
                limit: 4,
                sort: 'p_id',
                order: 'asc',
            },
            showLoading: 'hide',
            index: '',
            lastPage:false,//是否是最后一页
            loadingText:'加载中 ...',
            // title: '标题',
            content: '',
            confirmText: '确定',
            cancelText: '取消',
            show: false,
            single: false,
            showNoPrompt: false,
            isChecked: false,
            selectCar: '',
            selectNum: '',
        }
    },
    created () {
        let user = this.$storage.getSync('UserInfo')
        this.userId = user.id
        this.params.uid = user.id
        this.getCarList()
    },
    mounted () {
    },
    eros: {
        beforeBackAppear(params) {
            this.getCarList()
        }
    },
    methods: {
        // 返回
        back () {
            this.$router.back();
        },
        // 添加车辆
        addCar () {
            this.$router.open({
                name: 'acw.sellcar.sellcar',
                type: 'PUSH',
            });
        },
        // 汽车详情
        carDetail (id) {
            this.$router.open({
                name: 'acw.buycar.cardetail',
                type: 'PUSH',
                params: {carId: id}
            });
        },
        callPhone (phone) {
            this.$coms.call(phone)
        },
        
        loadingDown(index) {
            this.carArray = []
            this.params.page = 1
            this.getCarList()
        },
        onloading(index) {
            this.params.page = this.params.page + 1;
            this.$notice.loading.show();
            this.showLoading = 'show';
            if(!this.lastPage) {
                this.getCarList();
            }
            setTimeout(() => {
                this.$notice.loading.hide();
                this.showLoading = 'hide'
            }, 600)
        },
        // 查询车辆列表
        getCarList () {
            this.$notice.loading.show();
            this.$fetch({
                method: 'POST',    // 大写
                name: 'cars.searchMyCar', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: this.params
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log('查询车辆列表', JSON.stringify(resData.data))
                if(resData && resData.errno == 0){
                    this.carArray = this.getCarPic(resData.data.items)
                } else {
                }
                this.$notice.loading.hide();
                if(resData.data && this.carArray.length >= resData.data.total){
                    this.lastPage = true;
                    this.loadingText = '没有更多'
                }
                this.$notice.loading.hide();
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
                reject(error)
                // this.$notice.toast({ message: "登录失败, 请重试"})
            })
        },
        // 汽车图片
        getCarPic (list) {
            for (let i = 0, len = list.length; i < len; i++) {
                list[i].headImg = list[i].pPics.split(';')[1]
            }
            return list
        },
        // 删除汽车
        deleteCars (id) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'cars.delete', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'car_id': id}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '删除成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '删除失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 上架汽车
        shelfCars (car) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'cars.shelf', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'car_id': car.pId}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '上架成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '上架失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 下架汽车
        obtainedCars (car) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'cars.obtained', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'car_id': car.pId}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '下架成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '下架失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 修改汽车
        toUpdate (id) {
            this.$router.open({
                name: 'acw.sellcar.updatecar',
                type: 'PUSH',
                params: {carId: id}
            });
            return
        },
        openDialog (car, num) {
            if (num == 1) {
                if (car.isshow) {
                    return
                }
                // this.title = '上架';
                this.content = '确认上架改车辆吗？';
            } else if (num == 2) {
                if (!car.isshow) {
                    return
                }
                // this.title = '下架';
                this.content = '确认下架改车辆吗？';
            } else if (num == 3) {
                // this.title = '删除';
                this.content = '确认删除改车辆吗？';
            }
            this.show = true;
            this.selectCar = car;
            this.selectNum = num;
        },
        dialogCancelBtnClick () {
            this.show = false;
        },
        dialogConfirmBtnClick () {
            this.show = false;
            if (this.selectNum == 1) {
                this.shelfCars(this.selectCar)
            } else if (this.selectNum == 2) {
                this.obtainedCars(this.selectCar)
            } else if (this.selectNum == 3) {
                this.deleteCars(this.selectCar.pId)
            }
        },
    }
}
</script>
<style>
@import 'src/js/css/base.scss';
.iconfont {
    font-family: iconfont;
}
.wrapper{
    background-color: #fff;
}
.border-cell {
    background-color: #f2f3f4;
    width: 750px;
    height: 24px;
    align-items: center;
    justify-content: center;
    border-bottom-width: 1px;
    border-style: solid;
    border-color: #e0e0e0;
}
.cell{
    background-color: #fff;
}

.content{
    flex-direction: row;
    height: 300px;
    border-bottom-width:1px;
    border-color: #e0e0e0;
    align-items: center;
    justify-content: center;
}
.content_first{
    width: 500px;
    color: #666;
    font-size: 30px;
    margin-bottom: 10px;
}
.content_img{
    height: 240px;
    width: 200px;
    margin-right: 10px;
}
.content_two{
    flex-direction: row;
    margin-top: 20px;
    margin-bottom: 20px;
}
.content_third{
    flex-direction: row;
}
.content_two1{
    color: #666;
    font-size: 30px;
    padding-left: 20px;
    padding-right: 20px;
}
.content_two2{
    color: #666;
    font-size: 30px;
    padding-left: 20px;
    padding-right: 20px;
    border-left-style: solid;
    border-left-width: 2px;
    border-left-color: #666;
    border-right-style: solid;
    border-right-width: 2px;
    border-right-color: #666;
}
.content_third_price{
    font-size: 30px;
    color: #1E90FF;
    margin-left: 20px;
    margin-right: 40px;
}
.content_four{
    width: 520px;
    flex-direction: row;
    justify-content: space-around;
    padding-top: 10px;
}
.content_four_text{
    flex: 1;
    font-size: 30px;
    color: #009100;
    border-style: solid;
    border-width: 1px;
    border-color: #009100;
    border-radius: 5px;
    height: 60px;
    line-height: 60px;
    margin-left: 10px;
    margin-right: 10px;
}
.cont_background{
    background-color: #bbb;
}
.item-container {
    width: 750px;
    background-color: #f2f3f4;
}
.no-order {
    justify-content: center;
    align-items: center;
    height: 1034px;
}
</style>
